<?php
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	if(!empty($_RQ['code'])){
		$params['code']=trim($_RQ['code']);
		$return['params']['code']=trim($_RQ['code']);
	}
    $fields=array();
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_yxgoodserpfw_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['cangdi']=json_decode($item['cangdi'],true);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['type']=array('1'=>'直接添加','2'=>'其他插件调用接口添加');		
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品管理'),
			array('op'=>'edit','title'=>'添加商品'),
		),
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：如果已经添加了防伪商品，添加防伪批次，请勿修改：参数设置--防伪码生成设置。否则可能导致，已生成的批次不可用！只能删除商品批次重来！','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'code','title'=>'商品编号','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'cangdi','title'=>'产地','type'=>'pcd'),
			array('name'=>'pici','title'=>'生成批次','type'=>'string'),
			array('name'=>'cangday','title'=>'出厂日期','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'type'),
		),
		'action'=>array(
			array('type'=>'href','op'=>'params','title'=>'更多自定义内容'),
			array('type'=>'href','action'=>'batch','title'=>'批次管理'),
			array('type'=>'post','op'=>'copy','title'=>'复制商品'),
		)
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
			array('name'=>'code','width'=>15,'title'=>'商品编号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxgoodserpfw_goods',array('id'=>$id,'merchid'=>$_SESSION['onemerchid']));
	}else{
		$item=array('isweishop'=>0);
	}
	$item['cangdi']=json_decode($item['cangdi'],true);
	$item['diytext']=json_decode($item['diytext'],true);
	$item['images']=json_decode($item['images'],true);
	$item['params']=$item['params']?json_decode($item['params'],true):array();
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'模板设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品管理'),
			array('op'=>'edit','title'=>'添加商品')
		),
		'edit'=>array(
			array('name'=>'isweishop','title'=>'商城商品？','type'=>'switch'),
			array('name'=>'goodsid','title'=>'选择商品','type'=>'choose','action'=>'jy_weishop/goods.get_goods//nomerch=1','show'=>array('isweishop'=>1)),
			array('name'=>'title','title'=>'名称','type'=>'string','show'=>array('isweishop'=>0)),
			array('name'=>'images','title'=>'图片','type'=>'images','show'=>array('isweishop'=>0)),
			array('name'=>'price','title'=>'价格','type'=>'string','show'=>array('isweishop'=>0)),
			array('name'=>'url','title'=>'商品链接','type'=>'string','show'=>array('isweishop'=>0)),
			array('name'=>'cangdi','title'=>'产地','type'=>'pcd'),
			array('name'=>'pici','title'=>'生成批次','type'=>'string','help'=>'例如：20200101'),
			array('name'=>'cangday','title'=>'出厂日期','type'=>'time','time_type'=>'day'),
			array(
							'type'=>'array',
							'title'=>'更多字段参数',
							'name'=>'diytext',
							'add_tip'=>'添加一个字段',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'参数名称'),
									array('type'=>'string','name'=>'value','title'=>'参数内容'),
									
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'参数名称','help'=>'例如：产品规格、生产许可证、产品标准代号、保质期等'),
									array('type'=>'string','name'=>'value','title'=>'参数内容'),
								)
			),
			array('name'=>'content','title'=>'商品描述','type'=>'richtext'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['isweishop']){
		$data=pdo_get('jy_weishop_goods',array('id'=>$postdata['goodsid']),array('title','images','image','price','subtitle'));	
		$data['goodsid']=$postdata['goodsid'];
		$data['pici']=$postdata['pici'];
		$data['cangday']=$postdata['cangday'];
		$data['cangdi']=ijson_encode($postdata['cangdi']);
		$data['diytext']=ijson_encode($postdata['diytext']);
		$data['isweishop']=1;
		$data['content']=$postdata['content'];
		unset($data['subtitle']);
	}else{
		$data=array_fields(array('title','price','images','content','url','goodsid','isweishop','pici','cangday','cangdi'),$postdata);
		$data['cangdi']=ijson_encode($postdata['cangdi']);
		$data['diytext']=ijson_encode($postdata['diytext']);
		$data['image']=$data['images'][0];
		$data['images']=ijson_encode($data['images']);
	}
	$data['params']=ijson_encode($postdata['params']);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_yxgoodserpfw_goods',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['type']=1;
		$this->model('goods')->add($data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='copy'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择商品！','error');
	}
	$goods=pdo_get('jy_yxgoodserpfw_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	unset($goods['id']);
	$this->model('goods')->add($goods);
	exi('复制商品成功！');
}
if($_RQ['op']=='params'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxgoodserpfw_goods',array('id'=>$id,'merchid'=>$_SESSION['onemerchid']));
		$_SESSION['goodsid']=$id;
	}else{
		$item=pdo_get('jy_yxgoodserpfw_goods',array('id'=>$_SESSION['goodsid'],'merchid'=>$_SESSION['onemerchid']));
		if(!$item){
			exi('请选择商品！','error');
		}
	}
	$list=$item['params']?json_decode($item['params'],true):array();
	foreach($list as $k=>&$lv){
		$lv['id']=$k+1;
		$lv['desc2']='进入编辑查看详情';
	}
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'params','title'=>'自定义内容管理'),
			array('op'=>'paradd','title'=>'添加自定义内容')
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：例如：生产环境，生产流程等.','edit_op'=>'paradd','del_op'=>'pardel','list'=>array(
			array('type'=>'string','name'=>'paixu','title'=>'排序'),
			array('type'=>'string','name'=>'title','title'=>'内容标题'),
			array('type'=>'string','name'=>'desc2','title'=>'内容详情'),	
		),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='paradd'){
	$item=pdo_get('jy_yxgoodserpfw_goods',array('id'=>$_SESSION['goodsid'],'merchid'=>$_SESSION['onemerchid']));
	if(!$item){
		exi('请选择商品！','error');
	}
	$id=intval($_RQ['id']);
	$list=$item['params']?json_decode($item['params'],true):array();
	if($id){
		$k=$id-1;
		foreach($list as $k2=>$sv){
			if($sv['id']==$id || $k==$k2){
				$sv['id']=$id;
				$item=$sv;
			}
		}
	}else{
		$item=array();
	}
	$item['id']=$id?:'';
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'postpar',
		'desc'=>'自定义内容设置',
		'tabs'=>array(
			array('op'=>'params','title'=>'自定义内容管理'),
			array('op'=>'paradd','title'=>'添加自定义内容')
		),
		'edit'=>array(
			array('type'=>'string','name'=>'paixu','title'=>'排序'),
			array('type'=>'string','name'=>'title','title'=>'内容标题'),	
			array('type'=>'richtext','name'=>'desc','title'=>'内容详情'),	
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='postpar'){
	$postdata=get_postdata();
	$item=pdo_get('jy_yxgoodserpfw_goods',array('id'=>$_SESSION['goodsid'],'merchid'=>$_SESSION['onemerchid']));
	if(!$item){
		exi('请选择商品！','error');
	}
	$id=intval($postdata['id']);
	$params=$item['params']?json_decode($item['params'],true):array();
	if($id){
		$k=$id-1;
		foreach($params as $k2=>&$sv){
			if($sv['id']==$id || $k==$k2){
				$sv=array(
					'paixu'=>$postdata['paixu'],
					'title'=>$postdata['title'],
					'desc'=>$postdata['desc'],
				);
			}
		}
	}else{
		$params[]=array(
				'paixu'=>$postdata['paixu'],
				'title'=>$postdata['title'],
				'desc'=>$postdata['desc'],
		);
	}
	$params=array_values($params);
	pdo_update('jy_yxgoodserpfw_goods',array('params'=>ijson_encode($params)),array('id'=>$_SESSION['goodsid'],'merchid'=>$_SESSION['onemerchid']));
	exi('设置成功','','params');
}
if($_RQ['op']=='pardel'){
	$item=pdo_get('jy_yxgoodserpfw_goods',array('id'=>$_SESSION['goodsid'],'merchid'=>$_SESSION['onemerchid']));
	if(!$item){
		exi('请选择商品！','error');
	}
	$id=intval($_RQ['id']);
	$params=$item['params']?json_decode($item['params'],true):array();
	if($id){
		$k=$id-1;
		foreach($params as $k2=>$sv){
			if($sv['id']==$id || $k==$k2){
				unset($params[$k]);
			}
		}
	}else{
		exi('请选择内容！','error');
	}
	$params=array_values($params);
	pdo_update('jy_yxgoodserpfw_goods',array('params'=>ijson_encode($params)),array('id'=>$_SESSION['goodsid'],'merchid'=>$_SESSION['onemerchid']));
	exi('删除成功','','params');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_yxgoodserpfw_goods',array('id'=>$id,'merchid'=>$_SESSION['onemerchid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_yxgoodserpfw_goods',array('id'=>$id,'merchid'=>$_SESSION['onemerchid']));
			}
		}
	}
	exi('删除成功！');
}

?>